import java.io.*;
import java.util.*;

public class Record implements Comparable<Record>, Serializable{
	
	//Data Members
	private String _peerName = null;
	private String _ip = null;
	private int _tcp = -1;
	private int _udp = -1;
	private String _phone = null;
	private String _email = null;
	private Date _lastConnectionTime = null;
	private int _status = 0;
	private Date _peerVersion = null;
	private Date _dbVersion = null;
	private int _picSize = 0;
	private Date _picVersion = null;
	private int _rcvWin;
	private Date _lastUpdate = null;
	private int _tries = 0;
	
	public Record clone() {
		Record retVal =  new Record((Date)_dbVersion.clone(), 
				new String(_email), 
				new String(_ip), 
				(Date)_lastConnectionTime.clone(), 
				new String(_peerName),
				(Date)_peerVersion.clone(), 
				new String(_phone), 
				_picSize, 
				(Date)_picVersion.clone(), 
				_rcvWin, 
				_status, 
				_tcp, 
				_udp,
				(Date)_lastUpdate.clone());
		retVal._tries = _tries;
		
		return retVal;
	}

	private static final long serialVersionUID = -1244157354422754982L;

	//Constructor
	public Record(Date dbVersion, String email, String ip,
			Date lastConnectionTime, String peerName, Date peerVersion, String phone,
			int picSize, Date picVersion, int rcvWin, int status,
			int tcp, int udp, Date lastUpdate) {
		
		_dbVersion = dbVersion;
		this._email = email == null ? " " : email;
		this._ip = ip == null ? " " : ip;
		_lastConnectionTime = lastConnectionTime;
		_peerName = peerName == null ? " " : peerName;
		_peerVersion = peerVersion;
		this._phone = phone == null ? " " : phone;
		_picSize = picSize;
		_picVersion = picVersion;
		_rcvWin = rcvWin;
		this._status = status;
		this._tcp = tcp;
		this._udp = udp;
		this._lastUpdate = lastUpdate;
		
	}
	
	//Constructor
	public Record(String peerName) {
		this(Utils.zeroDate()," "," ", Utils.zeroDate(), peerName, Utils.zeroDate(),
			" ",0, Utils.zeroDate(), 1, 1, -1, -1, Utils.zeroDate());
	}

	
	//Constructor
	public Record(String dbVersion, String peerVersion, String peerName, String ip,
			String tcp, String udp, String phone, String email, String lastConnectionTime,
			String status, String picSize, String picVersion) {
		
		_dbVersion = Utils.StringToDate(dbVersion);
		_peerVersion = Utils.StringToDate(peerVersion);
		_peerName = peerName == null ? " " : peerName;
		_ip = ip == null ? " " : ip;
		_tcp = Integer.parseInt(tcp);
		_udp = Integer.parseInt(udp);
		_phone = phone == null ? " " : phone;
		_email = email == null ? " " : email;
		_lastConnectionTime = Utils.StringToDate(lastConnectionTime);
		_status = Integer.parseInt(status);
		_picSize = Integer.parseInt(picSize);
		_picVersion = Utils.StringToDate(picVersion);
		_tries = 0;
		_lastUpdate = Utils.zeroDate();
	}
	
	@Override
	public int hashCode() {return _peerName.hashCode();}
	
	
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Record))
			return false;
		
		Record val = (Record)obj;

		return _peerName.equals(val._peerName);
	}

	public int compareTo(Record other) {
		return _peerName.compareTo(other._peerName);
	}
	/* Getters and Setters */
	
	public int get_tries() {return _tries;}
	public void set_tries(int newTries) {_tries = newTries;}
	
	public void inc_tries() {++_tries;}
	public void setOnline() {
		_tries = 0;
		_status = 1;
	}
	public void setOffline() {
		_status = 0;
		_tries = 0;
	}
	
	public void set_peerName(String _peerName) {
		this._peerName = _peerName;
	}
	public String get_peerName() {
		return _peerName;
	}
	
	public void set_ip(String _ip) {
		this._ip = _ip;
	}
	public String get_ip() {
		return _ip;
	}
	
	public void set_tcp(int _tcp) {
		this._tcp = _tcp;
	}
	public int get_tcp() {
		return _tcp;
	}
	
	public void set_udp(int _udp) {
		this._udp = _udp;
	}
	public int get_udp() {
		return _udp;
	}
	
	public void set_phone(String _phone) {
		this._phone = _phone;
	}
	public String get_phone() {
		return _phone;
	}
	
	public void set_email(String _email) {
		this._email = _email;
	}
	public String get_email() {
		return _email;
	}
	
	public void set_lastConnectionTime(Date lastConnectionTime) {
		this._lastConnectionTime = lastConnectionTime;
	}
	public void set_lastConnectionTime(long lastConnectionTime) {
		_lastConnectionTime.setTime(lastConnectionTime);
	}
	public Date get_lastConnectionTime() {
		return _lastConnectionTime;
	}
	
	public void set_status(int _status) {
		this._status = _status;
	}
	public int get_status() {
		return _status;
	}
	
	public void set_peerVersion(Date _peerVersion) {this._peerVersion = _peerVersion;}
	public Date get_peerVersion() {return _peerVersion;}
	
	public void set_dbVersion(Date _dbVersion) {this._dbVersion = _dbVersion;}
	public Date get_dbVersion() {return _dbVersion;}
	
	public void set_picSize(int _picSize) {
		this._picSize = _picSize;
	}
	public int get_picSize() {
		return _picSize;
	}
	
	public void set_picVersion(Date _picVersion) {this._picVersion = _picVersion;}
	public Date get_picVersion() {return _picVersion;}
	
	public void set_rcvWin(int _rcvWin) {
		this._rcvWin = _rcvWin;
	}
	public int get_rcvWin() {
		return _rcvWin;
	}

	public void set_lastUpdate(Date lastUpdate) {this._lastUpdate = lastUpdate;}
	public Date get_lastUpdate() {return _lastUpdate;}

	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append("$dbversion=" + Utils.DateToString(_dbVersion));
		sb.append(",$peerversion=" + Utils.DateToString(_peerVersion));
		sb.append(",$peername=" + _peerName);
		sb.append(",$ip=" + _ip);
		sb.append(",$tcp=" + _tcp);
		sb.append(",$udp=" + _udp);
		sb.append(",$phone=" + _phone);
		sb.append(",$email=" + _email);
		sb.append(",$lastConnectionTime=" + Utils.DateToString(_lastConnectionTime));
		sb.append(",$status=" + _status);
		sb.append(",$picSize=" + _picSize);
		sb.append(",$picVersion=" + Utils.DateToString(_picVersion));
		
		return sb.toString();
	}
}
